<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

    <%@include file="../common/include.jsp" %>

        <!DOCTYPE HTML>
        <html>

        <head>
            <base href="<%=basePath%>">
            <title>用户列表</title>


        </head>

        <body>





            <div class="row" style="margin-top:20px;margin-left:20px">
                <form id="searchForm" action="${pageContext.request.contextPath}/getUserList.html" method="post">

                    <div class="col-sm-4">

                        <div class="input-group">

                            <c:choose>
                                <c:when test="${ not empty pages.conditions.name }">
                                    <input type="text" class="form-control" value="${pages.conditions.name }" id="name" name="conditions['name']" maxlength="40">
                                </c:when>

                                <c:otherwise>
                                    <input type="text" class="form-control" placeholder="根据登录名或真实姓名模糊查询" id="name" name="conditions['name']" maxlength="40">
                                </c:otherwise>
                            </c:choose>



                            <span class="input-group-btn">
	   <button class="btn btn-primary" type="button"   id="btnsearch">查询</button>
	</span>


                        </div>
                    </div>

                    <div class="col-sm-2">
                        <a href="addUserInit.html" class="btn btn-primary">增加用户</a>
                    </div>

                </form>
            </div>



            <div style="margin-left:16px; margin-top:20px;margin-bottom:90px;margin-right:30px">
                <table class="table table-striped  table-bordered table-hover">
                    <thead>
                        <tr style="background-color:#dcdcdc; ">
                            <th>用户编号</th>
                            <th>登录账号</th>
                            <th>真实姓名</th>
                            <th>所属部门</th>
                            <!-- <th>出生日期</th> -->
                            <th>账号状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>

                    <tbody>


                        <c:forEach var="user" items="${pages.pageDatas}">






                            <tr id="tr_${user.userid }" <%-- <c:if test="${user.userid==1 and userid != 1 }"> style="display: none" </c:if>--%> >
                                <td>
                                    <c:out value="${user.userid}" />
                                </td>

                                <td>
                                    <c:out value="${user.username}" />
                                </td>
                                <td>
                                    <c:out value="${user.realname}" />
                                </td>
                                <td>
                                    <c:out value="${user.departmentname }" />
                                </td>

                                <td id="td_${user.userid}">
                                    ${ user.isUse }

                                </td>
                                <%-- 	<td>  <fmt:formatDate value="${user.birthday}" pattern="yyyy年MM月dd日" /> </td> --%>
                                    <td>
                                        <%-- <a href="getUserInfo.html?id=${user.id }"> 详情 </a> --%>
                                            <a href="updateUserInit.html?id=${user.userid }" title="修改" class="handleicon"> <i class="fa fa-pencil-square-o"></i> </a>
                                            <a href="setUserRightsInit.html?id=${user.userid }" title="定制权限" class="handleicon"> <i class="fa fa-key"></i> </a>




                                            <c:choose>
                                                <c:when test="${ user.isUse=='正常'}">
                                                    <span id="a_${user.userid }">  <a href="javascript:void(0)" onclick="changeUserFlag(${user.userid},2)" class="handleicon" title="停用"> <i class="fa fa-lock"></i>  </a>    </span>
                                                    <span id="b_${user.userid }" style="display:none">  <a href="javascript:void(0)" onclick="changeUserFlag(${user.userid},1)" class="handleicon" title="启用"> <i class="fa fa-unlock"></i>  </a>    </span>
                                                </c:when>

                                                <c:otherwise>
                                                    <span id="a_${user.userid }">  <a href="javascript:void(0)" onclick="changeUserFlag(${user.userid},1)" class="handleicon" title="启用"> <i class="fa fa-unlock"></i>  </a>    </span>
                                                    <span id="b_${user.userid }" style="display:none">  <a href="javascript:void(0)" onclick="changeUserFlag(${user.userid},2)" class="handleicon" title="停用"> <i class="fa fa-lock"></i>  </a>    </span>

                                                </c:otherwise>
                                            </c:choose>




                                            <a href="javascript:void(0)" onclick="deleteuser(${user.userid})" class="handleicon" title="删除"> <i class="fa fa-user-times"></i> </a>



                                    </td>
                            </tr>





                        </c:forEach>
                    </tbody>
                </table>


                <!--  使用自己的 分页标签  前面三个是固定的必须参数 后面的url  是请求路径，可以根据情况  加或者不加参数-->

                <c:if test="${pages.totalPages>1 }">
                    <page:pager pageSize="${pages.pageSize}" currPage="${pages.currPage}" totalRecords="${pages.totalRecords}" url="${pageContext.request.contextPath}/getUserList.html?conditions['name']=${pages.conditions.name}" />

                </c:if>






            </div>


        </body>


        <script>
            $("#btnsearch").click(function() {

                if (checkChar($("#name").val())) {
                    layer.msg('搜索条件含有非法字符！');
                    return false;
                }


                $("#searchForm").submit();

            });
            /*    通过ajax 提交删除请求 ，根据返回结果 删除本地 表格里的数据，实现无刷新删除 */

            function deleteuser(id) {


                if (id < 3) {
                    layer.msg('系统超级管理员不能被删除！');
                    return;
                }

                if (confirm('真的要删除用户吗？')) {

                    $.ajax({
                        type: 'POST',
                        url: '${pageContext.request.contextPath}/deleteUser.html',
                        data: {
                            "userId": id
                        },
                        dataType: 'json',
                        success: function(data) {

                            if (data.code == 1) {
                                layer.msg(data.msg);
                                window.location.href = "getUserList.html"

                            } else {
                                layer.msg(data.msg)

                            }


                        },
                        error: function() {
                            alert("出错");
                        }

                    })

                }


            }



            function changeUserFlag(id, flag) {


                if (confirm("是否真的需要修改用户状态?")) {

                    var textString;
                    var aString;
                    if (flag == 1) {
                        textString = "正常";
                        aString = $("#a_" + id).html();
                        bString = $("#b_" + id).html();
                    } else {
                        textString = " 停用";
                        aString = $("#a_" + id).html();
                        bString = $("#b_" + id).html();


                    }

                    $.ajax({
                        type: 'POST',
                        url: '${pageContext.request.contextPath}/changeUserFlag.html',
                        data: {
                            "userId": id,
                            "flag": flag
                        },
                        dataType: 'json',
                        success: function(msg) {

                            if (msg.code == 1) {
                                $("#td_" + id).text(textString);
                                $("#a_" + id).html(bString);
                                $("#b_" + id).html(aString);
                                layer.msg(msg.msg);
                            } else {
                                layer.msg(msg.msg);
                            }
                        },
                        error: function() {
                            alert("出错");
                        }

                    })

                }


            }
        </script>



        </html>